package com.example.computer_exam_training_sys.controller;

import com.example.computer_exam_training_sys.dto.LearningProgressDTO;
import com.example.computer_exam_training_sys.service.UserLearningProgressService;
import com.example.computer_exam_training_sys.vo.LearningProgressVO;
import com.example.computer_exam_training_sys.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/learning-progress")
public class UserLearningProgressController {

    @Autowired
    private UserLearningProgressService service;

    /**
     * 自动更新用户所有课程的学习进度
     */
    @PostMapping("/auto-update/{userId}")
    public Result<String> autoUpdateAllProgress(@PathVariable Long userId) {
        try {
            service.autoUpdateAllProgress(userId);
            return Result.success("所有课程学习进度已自动更新");
        } catch (Exception e) {
            return Result.fail("更新失败: " + e.getMessage());
        }
    }

    @GetMapping("/course/{userId}")
    public Result<List<LearningProgressDTO>> getCourseProgress(@PathVariable Long userId) {
        return Result.success(service.getCourseProgress(userId));
    }

    @GetMapping("/course/{userId}/{courseId}")
    public Result<LearningProgressDTO> getCourseLearningProgress(
            @PathVariable Long userId,
            @PathVariable Long courseId) {
        return Result.success(service.getCourseLearningProgress(userId, courseId));
    }

    @PostMapping("/course/{userId}/{courseId}")
    public Result<Void> updateCourseLearningProgress(
            @PathVariable Long userId,
            @PathVariable Long courseId,
            @RequestParam Double completionRate,
            @RequestParam Integer studyTime) {
        service.updateCourseLearningProgress(userId, courseId, completionRate, studyTime);
        return Result.success();
    }

    @PostMapping("/update-plan/{userId}/{courseId}")
    public Result<Void> updateStudyPlanProgress(
            @PathVariable Long userId,
            @PathVariable Long courseId) {
        service.updateStudyPlanProgress(userId, courseId);
        return Result.success();
    }

    @GetMapping("/completion-rate/{userId}/{courseId}")
    public Result<Double> calculateCourseCompletionRate(
            @PathVariable Long userId,
            @PathVariable Long courseId) {
        return Result.success(service.calculateCourseCompletionRate(userId, courseId));
    }

    @GetMapping("/overview/{userId}")
    public Result<LearningProgressVO> getProgressOverview(@PathVariable Long userId) {
        return Result.success(service.getProgressOverview(userId));
    }

    @GetMapping("/weak-points/{userId}")
    public Result<LearningProgressVO> getWeakPointsAnalysis(@PathVariable Long userId) {
        return Result.success(service.getWeakPointsAnalysis(userId));
    }
}